package com.nightonke.boommenu;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.Color;
import com.nightonke.boommenu.Animation.AnimationManager;


/**
 * Created by Weiping Huang at 11:47 on 2017/5/15
 * For Personal Open Source
 * Contact me at 2584541288@qq.com or nightonke@outlook.com
 */

//@SuppressLint("ViewConstructor")
class BackgroundView extends StackLayout {

    private Color dimColor;
    private AnimatorProperty ap;

    protected BackgroundView(Context context, final BoomMenuButton bmb) {
        super(context);

        dimColor = bmb.getDimColor();

        ComponentContainer rootView = bmb.getParentView();
        ComponentContainer.LayoutConfig params = new ComponentContainer.LayoutConfig(
                rootView.getWidth(),
                rootView.getHeight());
        setLayoutConfig(params);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(RgbColor.fromArgbInt(Color.LTGRAY.getValue()));
        setBackground(element);
        setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                bmb.onBackgroundClicked();
            }
        });
        //setMotionEventSplittingEnabled(false);
        rootView.addComponent(this);
    }

    protected void reLayout(final BoomMenuButton bmb) {
        ComponentContainer rootView = bmb.getParentView();
        StackLayout.LayoutConfig params = (LayoutConfig) getLayoutConfig();
        params.width = rootView.getWidth();
        params.height = rootView.getHeight();
        setLayoutConfig(params);
    }
    public void clearAnimation() {
        try {
            if (ap != null && ap.isRunning()) {
                ap.reset();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    protected void dim(long duration, Animator.StateChangedListener completeListener) {
        setVisibility(VISIBLE);
        ap = this.createAnimatorProperty();
        ap.setDelay(0).setDuration(duration).alphaFrom(0).alpha(1);
        ap.setTarget(this);
        if (completeListener != null) ap.setStateChangedListener(completeListener);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(RgbColor.fromArgbInt(dimColor.getValue()));
        setBackground(element);
        setAlpha(0);
        ap.start();
    }

    protected void light(long duration, Animator.StateChangedListener completeListener) {
        ap = this.createAnimatorProperty();
        ap.setDelay(0).setDuration(duration).alphaFrom(1).alpha(0);
        ap.setTarget(this);
        if (completeListener != null) ap.setStateChangedListener(completeListener);
        ap.start();
    }
}
